package com.zhiwang.planet.mvvm.model.migration

import android.util.Log
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

//比如User增加一个字段 foo
//2->3
class SampleMigration(startVersion: Int, endVersion: Int) : Migration(startVersion, endVersion) {

    val TAG = "SampleMigration"
    override fun migrate(database: SupportSQLiteDatabase) {

        Log.i(TAG, "migrate: ---start------------")
        database.execSQL("CREATE TABLE IF NOT EXISTS `User1` ( `userId` TEXT NOT NULL, `userName` TEXT NOT NULL, `level` TEXT NOT NULL, `avatar` TEXT NOT NULL, `phone` TEXT NOT NULL, `isAliBind` INTEGER NOT NULL, `isWeiXinBind` INTEGER NOT NULL, `weixinNickname` TEXT NOT NULL, `aliAccountName` TEXT NOT NULL, `aliRealName` TEXT NOT NULL, `hasSetPaypassword` INTEGER NOT NULL, PRIMARY KEY(`phone`))")
        database.execSQL("INSERT INTO  `User1` SELECT `userId` , `userName` , `level`, `avatar`, `phone`, `isAliBind`, `isWeiXinBind`, `weixinNickname`, `aliAccountName`, `aliRealName`,`hasSetPaypassword`FROM User")
        database.execSQL("DROP TABLE `User`")
        database.execSQL("ALTER TABLE `User1` RENAME TO `User`")

        Log.i(TAG, "migrate: ---success------------")

    }
}